package com.feilx.changggou.order.mq;


import com.changgou.mq.MQConstant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/*
*   用户处理 定时订单业务
*
*   死信队列   队列销毁时候 会执行一个任务
*
* */

@Configuration
public class QueueConfig {

       // 监听队列
    @Bean(MQConstant.ORDER_QUEUE_DLATE)
    public Queue orderQueueDead(){
        return   new Queue(MQConstant.ORDER_QUEUE_DLATE);
    }

    // 死信队列
    @Bean(MQConstant.ORDER_QUEUE_LISTENER)
    public Queue orderQueueListener(){
        Map<String, Object> args = new HashMap<>(2);
//       x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", MQConstant.ORDER_DEAD_EXCHANGE);
//       x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", MQConstant.ORDER_DEAD_ROUTINGKEY);
        return QueueBuilder.durable(MQConstant.ORDER_QUEUE_LISTENER).withArguments(args).build();

    }


    // 交换机
    @Bean(MQConstant.ORDER_LISTENER_EXCHANGE)
    public Exchange oderExchangeListener(){
        return ExchangeBuilder.directExchange(MQConstant.ORDER_LISTENER_EXCHANGE).build();
    }

    // 交换机
    @Bean(MQConstant.ORDER_DEAD_EXCHANGE)
    public Exchange oderExchangeDead(){
        return ExchangeBuilder.directExchange(MQConstant.ORDER_DEAD_EXCHANGE).build();
    }


    // 绑定交换机
    @Bean
    public Binding binding2(@Qualifier(MQConstant.ORDER_LISTENER_EXCHANGE) Exchange exchange,
                           @Qualifier(MQConstant.ORDER_QUEUE_LISTENER) Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(MQConstant.ORDER_LISTENER_ROUTINGKEY).noargs();

    }

    // 绑定交换机
    @Bean
    public Binding binding(@Qualifier(MQConstant.ORDER_DEAD_EXCHANGE) Exchange exchange,
                           @Qualifier(MQConstant.ORDER_QUEUE_DLATE) Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(MQConstant.ORDER_DEAD_ROUTINGKEY).noargs();

    }


}
